package com.hbisdt.cet.sample.controller;

import com.hbisdt.cet.sample.entity.dto.CarbonArticleRequest;
import com.hbisdt.cet.sample.entity.po.CarbonArticle;
import com.hbisdt.cet.sample.service.impl.APICarbonArticleService;
import jakarta.annotation.Resource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * 路由控制器-用于演示基于API的Redis缓存
 * @Author Jiam
 * @Date 2025-09-06 21:47
 */
//@Controller
public class APIRouteController {
    @Resource
    private APICarbonArticleService apiCarbonArticleService;

    /**
     * 路由页面-登录页
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @GetMapping("/view/login")
    public String login() {
        return "login";
    }

    /**
     * 路由页面-列表
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @GetMapping("/view/index")
    public String articleList(Model model) {
        List<CarbonArticle> articleList = apiCarbonArticleService.getCarbonArticleList();
        model.addAttribute("articleList", articleList);
        return "articleList";
    }

    /**
     * 路由页面-新增
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @GetMapping("/view/insert")
    public String insertPage() {
        return "insertPage";
    }

    /**
     * 路由页面-编辑
     *
     * @return java.lang.String
     * @author : 教学示例
     */
    @GetMapping("/view/edit/{articleId}")
    public String editPage(@PathVariable Long articleId, Model model) {
        CarbonArticle article = apiCarbonArticleService.getById(articleId);
        model.addAttribute("article", article);
        return "editPage";
    }

    /**
     * 新增资讯
     *
     * @param carbonArticleRequest 请求实体
     * @return java.lang.String
     * @author : 教学示例
     */
    @PostMapping("/carbon/insert")
    public String insertData(CarbonArticleRequest carbonArticleRequest) {
        apiCarbonArticleService.addCarbonArticle(carbonArticleRequest);
        return "redirect:/view/index";
    }

    /**
     * 编辑资讯
     * @param carbonArticleRequest
     * @return java.lang.String
     * @author : 教学示例
     */
    @PostMapping("/carbon/edit")
    public String editData(CarbonArticleRequest carbonArticleRequest) {
        apiCarbonArticleService.editCarbonArticle(carbonArticleRequest);
        return "redirect:/view/index";
    }

    /**
     * 删除资讯
     *
     * @param articleId 资讯id
     * @return java.lang.String
     * @author : 教学示例
     */
    @GetMapping("/carbon/delete/{articleId}")
    public String deleteData(@PathVariable Long articleId) {
        CarbonArticleRequest request = new CarbonArticleRequest();
        request.setArticleId(articleId);
        apiCarbonArticleService.deleteCarbonArticle(request);
        return "redirect:/view/index";
    }
}
